/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.controller;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.vacoor.nothing.web.service.FileStorageService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * UEditor Config Controller
 * UEditor: http://fex-team.github.io/ueditor/
 *
 * @author vacoor
 */
@Controller
@RequestMapping("/")
public class UEditorController {
    public static final String ACTION_PARAM = "action";
    public static final String ACTION_CONFIG = "config";
    public static final String ACTION_UPLOAD_IMG = "upImg";
    public static final String UP_IMG_PARAM = "img";

    @Autowired
    private FileStorageService fileStorageService;

    @ResponseBody
    @RequestMapping(value = "ueditor")
    public Map<String, Object> invalid() {
        return ImmutableMap.<String, Object>of("state", "Invalid Request");
    }

    /**
     * UEditor server config
     */
    @ResponseBody
    @RequestMapping(value = "ueditor", params = {ACTION_PARAM + '=' + ACTION_CONFIG})
    public Map<String, Object> config(HttpServletRequest request) {
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("imageUrl", request.getContextPath() + "/storage/files/ul");
        retMap.put("imagePath", "/");
        // 上面两个好像没用
        retMap.put("imageActionName", ACTION_UPLOAD_IMG);   // must be
        retMap.put("imageFieldName", UP_IMG_PARAM);
        retMap.put("imageMaxSize", 3 * 1024 * 1024);    // 3m
        retMap.put("imageAllowFiles", Lists.newArrayList(".png", ".jpg", ".jpeg", ".gif", ".bmp"));
        retMap.put("imageUrlPrefix", request.getContextPath()); // must be, 访问图片时的 URL 前缀

        return retMap;
    }

    /**
     * UEditor upload image
     */
    @ResponseBody
    @RequestMapping(value = "ueditor", params = {ACTION_PARAM + '=' + ACTION_UPLOAD_IMG})
    public Map<String, Object> upload(
            @RequestParam(UP_IMG_PARAM) MultipartFile multipart,
            @RequestParam(value = "relative", required = false, defaultValue = "1") boolean relative,
            HttpServletRequest request) {
        Map<String, Object> retMap = Maps.newHashMap();

        if (null == multipart || multipart.isEmpty()) {
            return invalid();
        }

        try {
            String id = fileStorageService.store(multipart.getInputStream());

            String origName = multipart.getOriginalFilename();
            String suffix = "." + FilenameUtils.getExtension(origName);
            long size = multipart.getSize();
            String url = "/storage/files/" + id + suffix;

            if (!relative) {
                url = request.getContextPath() + url;
            }

            retMap.put("state", "SUCCESS");
            retMap.put("name", origName);
            retMap.put("original", origName);
            retMap.put("size", size);
            retMap.put("type", suffix);
            retMap.put("url", url);
        } catch (IOException ie) {
            retMap.put("state", "Upload Failed");
        }

        return retMap;
    }
}
